package com.vancouver.knows.ui.activity;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.view.HackyViewPager;

public class ImageViewPagerActivity extends BaseActivity {
	private static final String ISLOCKED_ARG = "isLocked";

	private ViewPager mViewPager;
	private List<PhotoView> photoViews = new ArrayList<PhotoView>();

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_view_pager);
		mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
		mViewPager.setBackgroundColor(getResources().getColor(R.color.black));
		setContentView(mViewPager);

		Intent intent = getIntent();
		if (intent == null) {
			return;
		}

		final List<String> urls = intent.getStringArrayListExtra("urls");
		if (urls == null || urls.size() == 0) {
			return;
		}

		int index = intent.getIntExtra("index", 0);

		for (String string : urls) {
			PhotoView photoView = new PhotoView(this);
			photoViews.add(photoView);
		}

		mViewPager.setAdapter(new SamplePagerAdapter(urls));
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				Picasso.with(imContext).load(urls.get(position)).into(photoViews.get(position));
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		mViewPager.setCurrentItem(index);

		if (savedInstanceState != null) {
			boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
			((HackyViewPager) mViewPager).setLocked(isLocked);
		}
	}

	private class SamplePagerAdapter extends PagerAdapter {

		private List<String> sDrawables;

		public SamplePagerAdapter(List<String> urls) {
			sDrawables = urls;
		}

		@Override
		public int getCount() {
			return sDrawables.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = photoViews.get(position);
			Picasso.with(imContext).load(sDrawables.get(position)).into(photoView);

			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

	private boolean isViewPagerActive() {
		return (mViewPager != null && mViewPager instanceof HackyViewPager);
	}

	@Override
	protected void onSaveInstanceState(@NonNull Bundle outState) {
		if (isViewPagerActive()) {
			outState.putBoolean(ISLOCKED_ARG, ((HackyViewPager) mViewPager).isLocked());
		}
		super.onSaveInstanceState(outState);
	}
}
